<?php
namespace app\admin\controller;
class Xuqi extends Common
{
    /*客户列表*/
    public function lst(){
        $aid = session('uid','','admin');
        $userInfo=db('admin')->where('id',$aid)->find();
        $group_id = $userInfo['group_id'];
        $groupMembers = $userInfo['group_members'];

        $xuqiRes=db('xuqi')->alias('x')->field('x.*,c.name as cname,a.name as aname,c.activity as cactivity,c.activity_time as cactivity_time')->join('client c','x.client_id=c.id')->join('admin a','x.aid=a.id');
        $count=db('xuqi');
        if($group_id == 5)
        {
            $groupMembers = $groupMembers?$groupMembers.','.$aid:$aid;
            $xuqiRes=$xuqiRes->where('c.xaid','IN',$groupMembers);
            $count=$count->where('aid','IN',$groupMembers);
        }
        else if($group_id == 2)
        {
            $xuqiRes=$xuqiRes->where('x.xaid',$aid);
            $count=$count->where('aid',$aid);
        }

        $xuqiRes=$xuqiRes->order('x.id desc')->paginate(10);
        $count=$count->count();
        $this->assign([
            'xuqiRes'=>$xuqiRes,
            'count'=>$count,
        ]);
        return view();
    }

    public function view(){
        if(input('id')){
            $id = input('id');
            $info = db('xuqi')
                    ->alias('x')
                    ->field('x.*,c.name as cname,a.name as aname,c.activity as cactivity,c.activity_time as cactivity_time')
                    ->join('client c','x.client_id=c.id')
                    ->join('admin a','x.aid=a.id')
                    ->where('x.id',$id)
                    ->find();
            $this->assign([
                'info'=>$info,
            ]);
            return view();
        }
    }
    
    
    
    public function tg(){
        if(request()->isAjax()){
            $id = input('id');
            $status = input('status');
            if($status == 1){
                $xuqi = db('xuqi')->find($id);
                $client = db('client')->find($xuqi['client_id']);
                $end_time = $client['end_time'] + (86400 * $xuqi['time']);
                $save=db('client')->where('id',$client['id'])->update(['end_time'=>$end_time]);
                $save_xuqi = db('xuqi')->where('id',$id)->update(['status'=>1,'review_id'=>session('uid','','admin'),'review_time'=>time()]);
                if($save !== false && $save_xuqi !== false){
                    return ['msg'=>'审核成功,时间已加','icon'=>1];
                }else{
                    return ['msg'=>'审核失败','icon'=>2];
                }
            }else{
                $save_xuqi = db('xuqi')->where('id',$id)->update(['status'=>2,'review_id'=>session('uid','','admin'),'review_time'=>time()]);
                if($save_xuqi !== false){
                    return ['msg'=>'已拒绝','icon'=>1];
                }else{
                    return ['msg'=>'拒绝失败','icon'=>2];
                }
            }
            
            
            return;
        }
    }
    
    
    
    
    
    
    
    
    
    
    
}